package com.zmsk.zmio.handler;

import android.content.Context;
import android.util.Log;

import com.example.yfaceapi.GPIOManager;
import com.zmsk.zmio.inter.IIOHandler;
import com.zmsk.zmio.ysface88.YSGpioUtils;

public class YSFace88Handler implements IIOHandler {

    private static final String TAG = "YSFace88Handler";

    public YSFace88Handler(Context context) {
        initIOControl(context);
    }

    @Override
    public void initIOControl(Context context) {
        Log.d(TAG, "initIOControl");
        try {
            YSGpioUtils.upgradeRootPermissionForExport();
            if (YSGpioUtils.exportGpio(146)) {
                YSGpioUtils.upgradeRootPermissionForGpio(146);
            }
            YSGpioUtils.setGpioDirection(146, 0);
            if (YSGpioUtils.exportGpio(171)) {
                YSGpioUtils.upgradeRootPermissionForGpio(171);
            }
            YSGpioUtils.setGpioDirection(171, 0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stopIOControl(Context context) {

    }

    @Override
    public void noticeGateOpen(Context context, String idNumber) {
        Log.d(TAG, "noticeGateOpen");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullUpRelay();

    }

    @Override
    public void noticeGateClose(Context context) {
        Log.d(TAG, "noticeGateClose");

        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullDownRelay();
    }

    @Override
    public void noticeWhiteLedOn(Context context) {
        Log.d(TAG, "noticeWhiteLedOn");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullUpWhiteLight();
        YSGpioUtils.writeGpioValue(171, "0");
    }

    @Override
    public void noticeWhiteLedOff(Context context) {
        Log.d(TAG, "noticeWhiteLedOff");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullDownWhiteLight();
        YSGpioUtils.writeGpioValue(171, "1");
    }

    @Override
    public void noticeInfraredLedOn(Context context) {
        Log.d(TAG, "noticeInfraredLedOn");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullUpInfraredLed();

    }

    @Override
    public void noticeInfraredLedOff(Context context) {
        Log.d(TAG, "noticeInfraredLedOff");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullDownInfraredLed();
    }

    @Override
    public void noticeRedLedOn(Context context) {
        Log.d(TAG, "noticeRedLedOn");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullUpRedLight();
    }

    @Override
    public void noticeRedLedOff(Context context) {
        Log.d(TAG, "noticeRedLedOff");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullDownRedLight();
    }

    @Override
    public void noticeGreenLedOn(Context context) {
        Log.d(TAG, "noticeGreenLedOn");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullUpGreenLight();
    }

    @Override
    public void noticeGreenLedOff(Context context) {
        Log.d(TAG, "noticeGreenLedOff");
        GPIOManager manager = GPIOManager.getInstance(context);
        manager.pullDownGreenLight();
    }

    @Override
    public void handleSlientInstall(String path) {

    }
}
